package com.example.zhish.viewpager_demo001;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener{
    private List<View> list;
    private List<String> tabList;
    private ViewPager viewPager;
    private PagerTabStrip pagerTabStrip;
    private List<Fragment> fragmentList;
    private MyPagerAdapter myPagerAdapter;
    private MyFragmentPagerAdapter myFragmentPagerAdapter;
    private MyFragmentStateAdapter myFragmentStateAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = new ArrayList<View>();
        tabList = new ArrayList<String>();
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        pagerTabStrip = (PagerTabStrip) findViewById(R.id.tab);


        //标题栏的数据源
        tabList.add("第1页");
        tabList.add("第2页");
        tabList.add("第3页");
        tabList.add("第4页");
        //PagerAdapter的数据源
        View view1 = View.inflate(this, R.layout.view1, null);
        View view2 = View.inflate(this, R.layout.view2, null);
        View view3 = View.inflate(this, R.layout.view3, null);
        View view4 = View.inflate(this, R.layout.view4, null);
        list.add(view1);
        list.add(view2);
        list.add(view3);
        list.add(view4);
        //fragmentAdapter的数据源
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new Fragment1());
        fragmentList.add(new Fragment2());
        fragmentList.add(new Fragment3());
        fragmentList.add(new Fragment4());
        //pagerTabStrip设置属性
        pagerTabStrip.setBackgroundColor(Color.YELLOW);
        pagerTabStrip.setTextColor(Color.RED);
        pagerTabStrip.setDrawFullUnderline(false);
        pagerTabStrip.setTabIndicatorColor(Color.GREEN);

        myPagerAdapter = new MyPagerAdapter(list,tabList);
       // viewPager.setAdapter(myPagerAdapter);
        myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),fragmentList,tabList);
       // viewPager.setAdapter(myFragmentPagerAdapter);
        myFragmentStateAdapter = new MyFragmentStateAdapter(getSupportFragmentManager(),fragmentList,tabList);
        viewPager.setAdapter(myFragmentStateAdapter);
        viewPager.setOnPageChangeListener(this);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

        Toast.makeText(this,"当前是第"+(position+1)+"个页面",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
